package com.vmont.encodeapp;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vmont.encodeapp.activity.BaseActivity;
import com.vmont.encodeapp.activity.fragment.EventFragment;
import com.vmont.encodeapp.activity.fragment.MapFragment;
import com.vmont.encodeapp.activity.fragment.SysFragment;
import com.vmont.encodeapp.activity.fragment.VideoFragment;
import com.vmont.encodeapp.adapter.AlarmAdapter;
import com.vmont.encodeapp.bean.AlarmBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    private VideoFragment videoFragment;

    private MapFragment mapFragment;

    private EventFragment eventFragment;

    private SysFragment sysFragment;

    private Fragment mFragment;

    private RelativeLayout mLayout;

    @BindView(R.id.rl_video)
    RelativeLayout rlVideo;

    @BindView(R.id.rl_map)
    RelativeLayout rlMap;

    @BindView(R.id.rl_event)
    RelativeLayout rlEvent;

    @BindView(R.id.rl_sys)
    RelativeLayout rlSys;

    @BindView(R.id.rv_jingao_list)
    RecyclerView recyclerView;

    private List<AlarmBean> datas;

    private AlarmAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment();
        getDevs();
    }


    @OnClick({R.id.rl_video, R.id.rl_map, R.id.rl_event, R.id.rl_sys})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_video:
                switchFragment(videoFragment);
                rlVideo.setBackground(getDrawable(R.drawable.main_top_tab_bg));
                mLayout.setBackground(null);
                mLayout = rlVideo;
                break;
            case R.id.rl_map:
                if (mapFragment == null) {
                    mapFragment = new MapFragment();
                }
                switchFragment(mapFragment);
                rlMap.setBackgroundResource(R.drawable.main_top_tab_bg);
                mLayout.setBackground(null);
                mLayout = rlMap;
                break;
            case R.id.rl_event:
                if (eventFragment == null) {
                    eventFragment = new EventFragment();
                }
                switchFragment(eventFragment);
                rlEvent.setBackgroundResource(R.drawable.main_top_tab_bg);
                mLayout.setBackground(null);
                mLayout = rlEvent;
                break;
            case R.id.rl_sys:
                if (sysFragment == null) {
                    sysFragment = new SysFragment();
                }
                switchFragment(sysFragment);
                rlSys.setBackgroundResource(R.drawable.main_top_tab_bg);
                mLayout.setBackground(null);
                mLayout = rlSys;
                break;

        }
    }


    private void initFragment() {
        videoFragment = new VideoFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl_fragment, videoFragment);
        transaction.commitAllowingStateLoss();
        mFragment = videoFragment;
        rlVideo.setBackgroundResource(R.drawable.main_top_tab_bg);
        mLayout = rlVideo;

    }


    private void switchFragment(Fragment to) {
        try {
            if (mFragment != to) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                if (!to.isAdded()) {
                    transaction.hide(mFragment).add(R.id.fl_fragment, to).commitAllowingStateLoss();
                } else {
                    transaction.hide(mFragment).show(to).commitAllowingStateLoss();
                }
            }
            mFragment = to;
        } catch (Exception e) {

        }

    }


    private void getDevs() {
        datas = new ArrayList<>();
        AlarmBean alarmBean;
        for (int i = 0; i < 15; i++) {
            alarmBean = new AlarmBean();
            datas.add(alarmBean);
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new AlarmAdapter(R.layout.item_home_alarm, datas);
        adapter.addHeaderView(getView());
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private View getView() {
        View view = LayoutInflater.from(this).inflate(R.layout.alarm__top, null);
        return view;
    }
}
